package com.earl.eatbao.web;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.Goods;
import com.earl.eatbao.service.GoodsService;

import java.util.ArrayList;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * @author Administrator
 * @version 1.0
 * @created 23-十一月-2016 16:55:14
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

	// desc:日记工具logger
	private static final Logger logger = getLogger(GoodsController.class);

	@Autowired
	private GoodsService goodsService;

	/**
	 * 添加商品
	 * @param goods 商品对象
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "classification", value = "商品分类", required = true, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "defNumber", value = "商品默认数量", required = true, dataType = "float", paramType = "query"),
			@ApiImplicitParam(name = "introduce", value = "商品简介", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "name", value = "商品名", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "number", value = "商品数量", required = true, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "shopId", value = "商店ID", required = true, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "price", value = "商品单价", required = true, dataType = "Float", paramType = "query")
	})
	@ApiOperation(value = "添加商品", notes = "添加商品", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/add",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> addGoods(
	      Goods goods,
		  @ApiParam(name = "file", value = "商品图片") MultipartFile file){

		logger.info("<====== 进入addGoods:方法,{[goods, file] :" +  goods.toString() + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.addGoods(goods, file));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出addGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 删除商品
	 * @param goodId
	 * @return
	 */
	@ApiOperation(value = "删除商品", notes = "删除商品", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/delete/{goodId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> deleteGoods(
			@ApiParam(name="goodId", value="删除指商品ID",required= true)
			@PathVariable
			Long goodId){

		logger.info("<====== 进入deleteGoods:方法,{[goodId] : " + goodId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		goodsService.delete(goodId);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出deleteGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 修改商品信息
	 * @param goods 商品对象
	 * @return
	 */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classification", value = "商品分类", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "defNumber", value = "商品默认数量", required = true, dataType = "float", paramType = "query"),
            @ApiImplicitParam(name = "introduce", value = "商品简介", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "number", value = "商品数量", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "商品单价", required = true, dataType = "Float", paramType = "query")
    })
 	@ApiOperation(value = "修改商品信息", notes = "修改商品信息", httpMethod = "POST", response = String.class)
 	@RequestMapping(value="/update",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> updateGoods(Goods goods){
		logger.info("<====== 进入updateGoods:方法,{[goods] :" + goods + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		List<Goods> goodsList = new ArrayList<Goods>();
		goodsList.add(goods);
		result.getResultParm().put("goods", goodsService.update(goodsList).get(0));
		if (null != result.getResultParm().get("goods")) {
			result.setServiceResult(true);
			result.setResultInfo("请求操作成功");
		} else {
			result.setServiceResult(false);
			result.setResultInfo("请求操作失败");
		}
		result.setMessageCode(200l);

		logger.info("==== 退出updateGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 查找指定商品信息
	 * @param goodId
	 * @return
	 */

	@ApiOperation(value = "查找指定商品信息", notes = "查找指定商品信息", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/{goodId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> findGoods(
			@ApiParam(name="goodId", value="查找指定ID商品信息", required= true)
			@PathVariable Long goodId){
		logger.info("<====== 进入findGoods:方法,{[goodId] : " + goodId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(true);
		result.getResultParm().put("goods", goodsService.findOne(goodId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出findGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 商品审核通过
	 * 
	 * @param goodId 商品id
	 * @return
	 */
	@ApiOperation(value = "商品审核通过", notes = "商品审核通过", httpMethod = "POST")
	@RequestMapping(value="/pass/{goodId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> passGoodsCheck(
            @ApiParam(name="/goodId", value="审核通过", required= true)
            @PathVariable Long goodId) {
		logger.info("<====== 进入passGoodsCheck:方法,{[goodId] :" + goodId + "} ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.passGoodsCheck(goodId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出addGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商品审核失败
	 * 
	 * @param goodId 商品id
	 */

	@ApiOperation(value = "商品审核失败", notes = "UPDate fail", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/fail/{goodId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> failGoodsCheck(
            @ApiParam(name="failGoodsCheck", value="审核不通过", required= true)
            @PathVariable Long goodId) {
		logger.info("==== 退出addGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		goodsService.failGoodsCheck(goodId);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出failGoodsCheck方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 下架商品
	 * 
	 * @param goodId
	 */
	@ApiOperation(value = "下架商品", notes = "Change goods status", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/offShelves/{goodId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> shopSoldOutGoods(
			@ApiParam(name="goodId", value="下架商品id", required= true)
			@PathVariable Long goodId) {

		logger.info("<====== 进入offShelves:方法,{[goodId] :" + goodId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.shopSoldOutGoods(goodId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出addGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     * 商家上架商品
     * @param goodsId
     * @return
     */
	@ApiOperation(value = "商家上架商品", notes = "商家上架商品", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/onShelves/{goodsId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> shopPutAwayGoods(
			@ApiParam(name = "goodsId", required = true, value = "订单ID")
			@PathVariable Long goodsId) {

		logger.info("<====== 进入shopPutAwayGoods:方法,{[goodsId] : " + goodsId + "} ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.shopPutAwayGoods(goodsId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出shopPutAwayGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     * 管理员上架商品
     * @param goodsId
     * @return
     */
	@ApiOperation(value = "管理员上架商品", notes = "管理员上架商品", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/adminOnShelves/{goodsId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> adminPutAwayGoods(
			@ApiParam(name = "goodsId", required = true, value = "订单ID")
            @PathVariable Long goodsId) {
		logger.info("<====== adminPutAwayGoods:方法,{[goodsId] : " + goodsId + "} ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.adminPutAwayGoods(goodsId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== adminPutAwayGoods 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     * 管理员下架商品
     * @param goodsId
     * @return
     */
	@ApiOperation(value = "管理员下架架商品", notes = "管理员下架商品", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/adminOffShelves/{goodsId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> adminSoldOutGoods(
			@ApiParam(name = "goodsId", required = true, value = "订单ID")
			@PathVariable Long goodsId) {

		logger.info("<====== adminSoldOutGoods:方法,{[goodsId] : " + goodsId + "} ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.adminSoldOutGoods(goodsId));
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== adminSoldOutGoods 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}


    /**
     * 查询所有上架销售的商品
     * @return
     */
    @ApiOperation(value = "查询所用上架销售的商品", notes = "查询所用上架销售的商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/inuse",  method = RequestMethod.GET)
	public  ResponseEntity<ResultMessage> findAllUseGoods() {

        logger.info("<====== 进入findAllUseGoods:方法,{[] : } ====");
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.getResultParm().put("goodsList", goodsService.findOnShevlesGoods());
        result.setServiceResult(true);
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findAllUseGoods方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查看已经上架的商品
     * @return
     */
    @ApiOperation(value = "查看已经上架的商品", notes = "查看已经上架的商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/onShelves/all",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findByStatus() {
        logger.info("<====== 进入findByStatus:方法,{[] : } ====");
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.setServiceResult(true);
        result.getResultParm().put("goodsList", goodsService.findByStatus(1));//查看已经上架商品
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findByStatus方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    @ApiOperation(value = "查询未被审核浏览的商品", notes = "查询未被审核浏览的商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/waitcheck",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findByWatch() {
        logger.info("<====== 进入findByWatch:方法,{[] : } ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.getResultParm().put("goodsList", goodsService.findByWatch(false));
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findByWatch方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查询指定商店的所有商品
     * @param shopId
     * @return
     */
    @ApiOperation(value = "查询指定商店的所有商品", notes = "查询指定商店的所有商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/shop/{shopId}",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> shopAllGoods(@PathVariable Long shopId) {
    	// desc: 封装操作结果
    	ResultMessage result = new ResultMessage();
    	result.getResultParm().put("goodsList", goodsService.findShopGoods(shopId));
    	result.setMessageCode(200l);
    	result.setResultInfo("请求操作成功");
    	return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查询指定商店的所有商品
     * @param shopId
     * @return
     */
    @ApiOperation(value = "查询指定商店的未审核商品", notes = "查询指定商店的未审核商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/shop/waitcheck/{shopId}",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> shopWaitCheckGoods(@PathVariable Long shopId) {
    	// desc: 封装操作结果
    	ResultMessage result = new ResultMessage();
    	result.getResultParm().put("goodsList", goodsService.findShopUnCheckGoods(shopId));
    	result.setMessageCode(200l);
    	result.setResultInfo("请求操作成功");
    	return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查询所用商品
     * @return
     */
    @ApiOperation(value = "查询所有商品", notes = "查询所有商品", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/all",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findAll() {
    	// desc: 封装操作结果
    	ResultMessage result = new ResultMessage();
    	result.getResultParm().put("goodsList", goodsService.findAll());
    	result.setMessageCode(200l);
    	result.setResultInfo("请求操作成功");
    	return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

	/**
	 * 商店图片上传
	 * @param file
	 * @param goodsId
	 * @return
	 */
	@ApiOperation(value = "上传商品图片", notes = "上传商品图片", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/uploadImage",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> upLoadGoodImage(
			@ApiParam(name = "file", value = "上传文件")
			@RequestParam("file") MultipartFile file,
			@ApiParam(name = "goodsId", required = true, value = "商品ID")
			@RequestParam(name = "goodsId", required = true) Long goodsId) {
		logger.info("<====== 进入upLoadGoodImage:方法,{[file, goodsId] :" + goodsId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(goodsService.upLoadGoodImage(file, goodsId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出upLoadGoodImage方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}